{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import healpy as hp\n",
    "import matplotlib.pyplot as plt\n",
    "import numpy as np\n",
    "\n",
    "def make_plot(skymap=None, header=None):\n",
    "    header = dict(header)\n",
    "    for key in header:\n",
    "        print(key, ' : ', header[key])\n",
    "    #print(header)\n",
    "\n",
    "    if skymap is not None:\n",
    "        cmap = plt.cm.YlOrBr\n",
    "        cmap.set_under('w')\n",
    "        hp.mollview(skymap, title=\"\", cbar=True, notext=True, rot=180, hold=False, cmap=cmap)\n",
    "        hp.graticule()\n",
    "        # add some labels\n",
    "        plt.text(2.0,0., r\"$0^\\circ$\", ha=\"left\", va=\"center\")\n",
    "        plt.text(1.9,0.45, r\"$30^\\circ$\", ha=\"left\", va=\"center\")\n",
    "        plt.text(1.4,0.8, r\"$60^\\circ$\", ha=\"left\", va=\"center\")\n",
    "        plt.text(1.9,-0.45, r\"$-30^\\circ$\", ha=\"left\", va=\"center\")\n",
    "        plt.text(1.4,-0.8, r\"$-60^\\circ$\", ha=\"left\", va=\"center\")\n",
    "        plt.text(2.0, -0.15, r\"$0\\,\\mathrm{h}$\", ha=\"center\", va=\"center\")\n",
    "        plt.text(1.333, -0.15, r\"$4\\,\\mathrm{h}$\", ha=\"center\", va=\"center\")\n",
    "        plt.text(.666, -0.15, r\"$8\\,\\mathrm{h}$\", ha=\"center\", va=\"center\")\n",
    "        plt.text(0.0, -0.15, r\"$12\\,\\mathrm{h}$\", ha=\"center\", va=\"center\")\n",
    "        plt.text(-.666, -0.15, r\"$16\\,\\mathrm{h}$\", ha=\"center\", va=\"center\")\n",
    "        plt.text(-1.333, -0.15, r\"$20\\,\\mathrm{h}$\", ha=\"center\", va=\"center\")\n",
    "        plt.text(-2.0, -0.15, r\"$0\\,\\mathrm{h}$\", ha=\"center\", va=\"center\")\n",
    "        plt.draw()\n",
    "    \n",
    "        print(skymap)\n",
    "        print(skymap.min())\n",
    "        print(np.amin(skymap))\n",
    "        min_bin = np.where(skymap == np.amin(skymap))[0][0]\n",
    "        print(min_bin)\n",
    "    \n",
    "        theta, phi = hp.pix2ang(1024, min_bin)\n",
    "        print (-np.degrees(theta-np.pi/2.), np.degrees(phi))\n",
    "\n",
    "        #memfile = io.BytesIO()\n",
    "        plt.savefig('tmp.png', format = 'png', dpi = 150)\n",
    "        #memfile.seek(0)\n",
    "    print(len(header),len(skymap),type(skymap[0]))\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "skymap, header = hp.read_map('./fits_files/Run132043_80110393_nside1024.fits.gz',h=True, verbose=False)\n",
    "make_plot(skymap,header)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
